using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public enum E_LifeFun_Type
{
    Start,
    Update,
    LateUpdate,
    FixedUpdate,
    Enable,
    Disable,
    Destroy,
    OnTriggerEnter,
    OnTriggerExit,
    OnTriggerStay,
    OnCollisionEnter,
    OnCollisionStay,
    OnCollisionExit,

}


public class LuaMono : MonoBehaviour
{
    public event UnityAction startAction;
    public event UnityAction updateAction;
    public event UnityAction fixedUpdateAction;
    public event UnityAction lateUpdateAction;
    public event UnityAction onEnableAction;
    public event UnityAction onDisableAction;
    public event UnityAction destroyAction;

    public event UnityAction<object> onTriggerEnterAction;
    public event UnityAction<object> onTriggerExitAction;
    public event UnityAction<object> onTriggerStayAction;

    public event UnityAction<object> onCollisionEnterAction;
    public event UnityAction<object> onCollisionStayAction;
    public event UnityAction<object> onCollisionExitAction;

    void Start()
    {
        startAction?.Invoke();
    }

    // Update is called once per frame
    void Update()
    {
        updateAction?.Invoke();
    }

    void OnDestroy()
    {
        destroyAction?.Invoke();
        startAction = null;
        updateAction = null;
        fixedUpdateAction = null;
        lateUpdateAction = null;
        onEnableAction = null;
        onDisableAction = null;
        destroyAction = null;
        onTriggerEnterAction = null;
        onTriggerExitAction = null;
        onTriggerStayAction = null;
        onCollisionEnterAction = null;
        onCollisionStayAction = null;
        onCollisionExitAction = null;
    }

    void OnCollisionEnter(Collision other)
    {
        onCollisionEnterAction?.Invoke(other);
    }

    void OnCollisionStay(Collision other)
    {
        onCollisionStayAction?.Invoke(other);
    }



    void OnCollisionExit(Collision other)
    {
        onCollisionExitAction?.Invoke(other);
    }


    void OnTriggerEnter(Collider other)
    {
        onTriggerEnterAction?.Invoke(other);
    }


    void OnTriggerExit(Collider other)
    {
        onTriggerExitAction?.Invoke(other);
    }


    void OnTriggerStay(Collider other)
    {
        onTriggerStayAction?.Invoke(other);
    }


    void OnDisable()
    {
        onDisableAction?.Invoke();
    }

    void OnEnable()
    {
        onEnableAction?.Invoke();
    }

    void LateUpdate()
    {
        lateUpdateAction?.Invoke();
    }

    void FixedUpdate()
    {
        fixedUpdateAction?.Invoke();
    }


    public void AddOrRemoveOnCollisionListener(UnityAction<object> fun,E_LifeFun_Type type,bool isAdd = true)
    {
        switch(type)
        {
            case E_LifeFun_Type.OnTriggerEnter:
                if(isAdd)
                {
                    onTriggerEnterAction += fun;
                }
                else
                {
                    onTriggerEnterAction -= fun;
                }
            break;
            case E_LifeFun_Type.OnTriggerStay:
                if(isAdd)
                {
                    onTriggerStayAction += fun;
                }
                else
                {
                    onTriggerStayAction -= fun;
                }
            break;
            
            case E_LifeFun_Type.OnTriggerExit:
                if(isAdd)
                {
                    onTriggerExitAction += fun;
                }
                else
                {
                    onTriggerExitAction -= fun;
                }
            break;
            
            case E_LifeFun_Type.OnCollisionEnter:
                if(isAdd)
                {
                    onCollisionEnterAction += fun;
                }
                else
                {
                    onCollisionEnterAction -= fun;
                }
            break;
            
            case E_LifeFun_Type.OnCollisionStay:
                if(isAdd)
                {
                    onCollisionStayAction += fun;
                }
                else
                {
                    onCollisionStayAction -= fun;
                }
            break;

            case E_LifeFun_Type.OnCollisionExit:
                if(isAdd)
                {
                    onCollisionExitAction += fun;
                }
                else
                {
                    onCollisionExitAction -= fun;
                }
            break;
            
        }
    }


    public void AddOrRemoveListener(UnityAction fun,E_LifeFun_Type type,bool isAdd = true)
    {
        switch(type)
        {
            case E_LifeFun_Type.Start:
            {   
                if(isAdd)
                {
                    startAction += fun;
                }
                else
                {
                    startAction -= fun;
                }
            }
            break;
            case E_LifeFun_Type.Update:
                if(isAdd)
                {
                    updateAction += fun;
                }
                else
                {
                    updateAction -= fun;
                }
            break;
            case E_LifeFun_Type.LateUpdate:
                if(isAdd)
                {
                    lateUpdateAction += fun;
                }
                else
                {
                    lateUpdateAction -= fun;
                }
            break;
            case E_LifeFun_Type.FixedUpdate:
                if(isAdd)
                {
                    fixedUpdateAction += fun;
                }
                else
                {
                    fixedUpdateAction -= fun;
                }
            break;
            case E_LifeFun_Type.Enable:
                if(isAdd)
                {
                    onEnableAction += fun;
                }
                else
                {
                    onEnableAction -= fun;
                }
            break;
            case E_LifeFun_Type.Disable:
                if(isAdd)
                {
                    onDisableAction += fun;
                }
                else
                {
                    onDisableAction -= fun;
                }
            break;
            case E_LifeFun_Type.Destroy:
                if(isAdd)
                {
                    destroyAction += fun;
                }
                else
                {
                    destroyAction -= fun;
                }
            break;

        }
    }

}
